package com.zt.rongda.employee.controller;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.zt.servlet.*;
import com.zt.rongda.employee.model.Employee;
import com.zt.rongda.employee.service.EmployeeService;
import com.zt.system.model.Department;
import com.zt.system.service.IPermissionService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping(value = "/query")
    @ResponseBody
    @DeptPermPrepare(id = "empId", deptId = "deptId")
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(employeeService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    @DeptPermValidate(id = "empId", deptId = "deptId")
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeService.add(getEmployee(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    @DeptPermValidate(id = "empId", deptId = "deptId")
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeService.update(getEmployee(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    @DeptPermValidate(id = "empId", deptId = "deptId")
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeService.delete(reqBean.getInfo("empId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private Employee getEmployee(ReqBean reqBean) {
        Employee employee = new Employee();
        employee.setId(reqBean.getInfo("empId"));
        employee.setCode(reqBean.getInfo("empCode"));
        employee.setName(reqBean.getInfo("empName"));
        employee.setGender(reqBean.getInfo("gender"));
        employee.setMobile(reqBean.getInfo("mobile"));
        employee.setIdcard(reqBean.getInfo("idcard"));
        employee.setAddress(reqBean.getInfo("address"));
        employee.setDeptId(reqBean.getInfo("deptId"));
        employee.setPosId(reqBean.getInfo("posId"));
        employee.setPosName(reqBean.getInfo("posName"));
        employee.setHireDate(reqBean.getInfoDate("hireDate"));
        employee.setPostDate(reqBean.getInfoDate("postDate"));
        employee.setResignDate(reqBean.getInfoDate("resignDate"));
        employee.setStatus(reqBean.getInfo("empStatus"));
        employee.setOperator(reqBean.getUserId());
        employee.setRemark(reqBean.getInfo("remark"));
        return employee;
    }

    @RequestMapping({"/tree"})
    @ResponseBody
    public JSONObject empTree(@RequestAttribute("reqBean") ReqBean reqBean) {
        String deptId = reqBean.getInfo("deptId");
        String empId = reqBean.getUserId();
        JSONArray jsonArray = new JSONArray();
        ArrayList deptIds = new ArrayList();
        Iterator list = this.permissionService.getAuthedDepartments(empId, reqBean.getMenuId(), deptId).iterator();

        while(list.hasNext()) {
            Department jsonObject = (Department)list.next();
            deptIds.add(jsonObject.getId());
            JSONObject emp = new JSONObject();
            emp.put("id", "dept-" + jsonObject.getId());
            emp.put("pId", "dept-" + jsonObject.getParentId());
            emp.put("name", jsonObject.getName());
            emp.put("type", "dept");
            emp.put("deptid", jsonObject.getId());
            emp.put("posid", "");
            jsonArray.add(emp);
        }

        List list1 = this.employeeService.queryEmpOfDept(deptIds);
        Iterator jsonObject1 = list1.iterator();

        while(jsonObject1.hasNext()) {
            Employee emp1 = (Employee) jsonObject1.next();
            if(!emp1.getId().equals("1")) {
                JSONObject json = new JSONObject();
                json.put("id", emp1.getId());
                json.put("pId", "dept-" + emp1.getDeptId());
                json.put("name", emp1.getName());
                json.put("type", "emp");
                json.put("deptid", emp1.getDeptId());
                json.put("posid", "");
                try {
                    json.put("searchKeys", emp1.getName() + " " + PinyinHelper.convertToPinyinString(emp1.getName(), "", PinyinFormat.WITHOUT_TONE) + " " +PinyinHelper.getShortPinyin(emp1.getName()));
                } catch (PinyinException e) {
                    e.printStackTrace();
                }
                jsonArray.add(json);
            }
        }

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("org", jsonArray);
        jsonObject2.put("pos", new JSONArray());
        return Response.response(jsonObject2);
    }

}